<?php

$id = empty($this->Param['id']) ? 0 : $this->Param['id'];
$this->Assign('id', $id);

$this->LoadHelper('BranchHelper');
$BranchHelper = new BranchHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('BankHelper');
$BankHelper = new BankHelper();

$this->LoadHelper('RoleHelper');
$RoleHelper = new RoleHelper();

if (empty($this->Param['option'])) {
    $data = $BranchHelper->branchRow($id);
    $data['br_bank_arr'] = empty($data['br_bank']) ? array() : explode(',', $data['br_bank']);
    $this->Assign('data', $data);
    //var_dump($data);
    //银行信息
    $bank = $BankHelper->bankAll(array('`bk_status` = ?' => 'ok'));
    $this->Assign('bank', $bank);
    //大区
    //拓展经理
    echo $this->GetView('edit.php');
} else {
    switch ($this->Param['option']) {
        case 'save':
            $where = array(
                '`br_code` = ? OR `br_name` = "' . $this->Param['br_name'] . '"' => $this->Param['br_code'],
            );
            if ($id > 0) {
                $where['`br_id` != ?'] = $id;
                $check = $BranchHelper->branchRow($where);
                if (!empty($check)) {
                    ErrorMsg::Debug('已存在当前名称或编号的网点');
                }
                $result = $BranchHelper->branchSave($this->Param, $id);
            } else {
                $check = $BranchHelper->branchRow($where);
                if (!empty($check)) {
                    ErrorMsg::Debug('已存在当前名称或编号的网点');
                }
                $result = $BranchHelper->branchSave($this->Param);
            }
            if ($result['success']) {
                $desc = $id > 0 ? '修改网点信息' : '新增网点信息';
                $MemberHelper->saveLog($this->UserConfig, $desc, json_encode($this->Param), $this->Controller . '_' . $this->Action);
                $data = $BranchHelper->branchRow($id);
                $msg = $BranchHelper->getAllMemeber($id);
                $data['br_bank_arr'] = empty($data['br_bank']) ? array() : explode(',', $data['br_bank']);
                $count = count($msg);
                for ($i = 0; $i < $count; $i++) {
                    $u_id = $msg[$i]['u_id'];
                    $u_bank_array = explode(',', $msg[$i]['u_bank']);
                    $new_u_bank = array_intersect($data['br_bank_arr'], $u_bank_array);
                    $u_data['u_bank'] = implode(',', $new_u_bank);
                    $MemberHelper->memberUpdate($u_data, $u_id);
                }
                ErrorMsg::Debug('保存成功', true);
            }
            ErrorMsg::Debug($result['msg']);
            break;
        case 'remove':
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            $result = $BranchHelper->branchRemove($id);
            if ($result) {
                $MemberHelper->saveLog($this->UserConfig, '删除网点', json_encode($this->Param), $this->Controller . '_' . $this->Action);
                ErrorMsg::Debug('删除成功');
            }
            ErrorMsg::Debug('删除失败');
            break;
        case 'userlist':
            $dp_id = empty($this->Param['dp_id']) ? ErrorMsg::Debug('参数错误') : $this->Param['dp_id'];
            $dp_id_info = explode('_', $dp_id);
            $this->Assign('dp_id', $dp_id);
            $ajax = $this->Param['ajax'];
            $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
            $rp = isset($this->Param['rp']) ? $this->Param['rp'] : 1000;
            $role_msg = $RoleHelper->roleName(array('`r_status` =?' =>'ok'));
            if ($ajax) {

                $where = array(
                    '`u_dp_type` = ?' => 'dot',
                    '`u_dp_id` = ?' => $dp_id_info[1],
                );
                $data = $MemberHelper->pageList($where, $page, $rp, $this->Param,'branch_userlist');
                $json_data = array('total' => $data['count'], 'rows' => array());
                if (!empty($data['all'])) {
                    foreach ($data['all'] as $rowNum => $row) {
                        $dpinfo = $MemberHelper->getDpinfo($row);
                        $row['u_dpname'] = $dpinfo['dpname'];
                        $row['u_addtime'] = date('Y-m-d H:i:s', $row['u_addtime']);
                        $row['u_role'] = $role_msg[$row['u_role']];
                        $row['u_status'] = $row['u_status'] == 'ok'? '正常':'注销';
                        $json_data['rows'][] = $row;
                    }
                }
                echo json_encode($json_data);
            } else {
                $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
                $this->Assign('member_info', $member_info);
                echo $this->GetView('userlist.php');
            }
            break;
        case 'auditconfig':
            $member_info = $this->LoadModel('Public')->getList('member','*',array('`u_dp_id` = ?' => 4));
            $branch_info = $this->LoadModel('Public')->getOneRow('branch','*',array('`br_id` = ?' => $id));
            for($i=0;$i<count($member_info);$i++){
                $member_name[] = $member_info[$i]['u_realname'];
            }
            $this->Assign('member_name',$member_name);
            $this->Assign('branch_info',$branch_info);
            echo $this->GetView('branch_auditconfig.php');
            break;
        case 'auditsave':
            $data['auditor'] = empty($this->Param['auditor']) ? '' : $this->Param['auditor'];
            $data['remark'] = empty($this->Param['audit_remark']) ? ErrorMsg::Debug('参数错误') : $this->Param['audit_remark'];
            $result = $BranchHelper->saveAuditConfig($id,$data);
            if ($result['success']) {
                ErrorMsg::Debug('保存成功', true);
            } else {
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'contractlist':
            $ajax = $this->Param['ajax'];
            //$br_id = empty($this->Param['dp_id']) ? ErrorMsg::Debug('参数错误') : $this->Param['dp_id'];
            $br_id = $this->Param['dp_id'];
            $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
            $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
            if ($ajax) {
                $where = array(
                    '`bc_brid` = ?' => $br_id,
                );
                $data = $BranchHelper->contractList($where, $page, $rp, $this->Param);
                $json_data = array('total' => $data['count'], 'rows' => array());
                if (!empty($data['all'])) {
                    foreach ($data['all'] as $rowNum => $row) {
                        $json_data['rows'][] = $row;
                    }
                }
                echo json_encode($json_data);
            } else {
                $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
                $this->Assign('member_info', $member_info);
                echo $this->GetView('branch_contractlist.php');
            }
            break;
        case 'contractadd':
            $branch_info = $this->LoadModel('Public')->getOneRow('branch','*',array('`br_id` = ?' => $id));
            $this->Assign('branch_info',$branch_info);
            echo $this->GetView('branch_contractadd.php');
            break;
        case 'contractsave':
            $data = $this->Param;
            $result = $BranchHelper->contractSave($data);
            if($result['success']){
                 ErrorMsg::Debug('保存成功', true);
            } else {
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case'contractedit':
            $id = empty($this->Param['bc_id'])?ErrorMsg::Debug('参数错误'):$this->Param['bc_id'];
            $contract_info = $BranchHelper->getContractinfo($id);
            $branch_info = $this->LoadModel('Public')->getOneRow('branch','*',array('`br_id` = ?' => $contract_info['bc_brid']));
            $contract_info['type'] = empty($this->Param['type'])? '' : $this->Param['type'];
            $branch_info['identifier'] = $contract_info['bc_cid'];
            $this->Assign('branch_info',$branch_info);
            $this->Assign('contract_info',$contract_info);
            echo $this->GetView('branch_contractadd.php');
        break;
        case 'contractremove':
            $cid = empty($this->Param['bc_id']) ? ErrorMsg::Debug('参数错误') : $this->Param['bc_id'];
            $result = $BranchHelper->contractRemove($cid);
            if($result){
                $MemberHelper->saveLog($this->UserConfig, '删除合同', json_encode($this->Param), $this->Controller . '_' . $this->Action);
                ErrorMsg::Debug('删除成功');
            }else{
                ErrorMsg::Debug('删除失败');
            }
            break;
        case 'closebranch':
            $br_id = empty($this->Param['brid']) ? ErrorMsg::Debug('参数错误') : $this->Param['brid'];
            $result = $MemberHelper->removeAll($br_id);
            if($result['success']){
                $MemberHelper->saveLog($this->UserConfig, '关闭网点所有人员', json_encode($this->Param), $this->Controller . '_' . $this->Action);
                ErrorMsg::Debug('关闭成功');
            }else{
                ErrorMsg::Debug('关闭失败');
            }
            break;
        case 'bankaccountedit':
            $br_id = empty($this->Param['brid']) ? ErrorMsg::Debug('参数错误') : $this->Param['brid'];
            $model = $this->LoadModel('Public');
            $branch_info = $model->getOneRow('branch','*',array('`br_id` = ?' => $br_id));
            $br_lead_msg = $model->getOneRow('member','*',array('`u_dp_type` = ?' => 'dot','`u_job` =?' =>'网点负责人','u_dp_id =?' => $br_id));
            $branch_info['br_mobile'] = $branch_info['br_mobile']>0? $branch_info['br_mobile'] : $br_lead_msg['u_mobile'];
            $this->Assign('branch_info',$branch_info);
            echo $this->GetView('branch_bankaccountedit.php');
            break;
        case 'bankaccountsave':
            $br_id = empty($this->Param['br_id']) ? ErrorMsg::Debug('参数错误') : $this->Param['br_id'];
            if(empty($this->Param['br_payee']) || empty($this->Param['br_acc_bank']) || empty($this->Param['br_acc_no'])){
                ErrorMsg::Debug('请将信息填写完整');
            }
            $result = $BranchHelper->bankaccSave($this->Param);
            if($result['success']){
                $MemberHelper->saveLog($this->UserConfig, '编辑网点银行账号', json_encode($this->Param), $this->Controller . '_' . $this->Action);
                ErrorMsg::Debug('保存成功',true);
            }else{
                ErrorMsg::Debug('保存失败');
            }
            break;
        case 'unadmittancelist':
            $br_id = empty($this->Param['id']) ? ErroMsg::Debug('参数错误') : $this->Param['id'];
            $all_data =  $BranchHelper->getAllPlace();
            $unadmittance_data = $BranchHelper->getAllPlace($br_id);
            $this->Assign('br_id',$br_id);
            $this->Assign('all_data',$all_data);
            $this->Assign('unadmit_data',$unadmittance_data);
            echo $this->GetView('branch_unadmittance.php');
            break;
        case 'unadmittancesave':
            $old_data = $BranchHelper->takeoutId($this->Param['br_id']);
            $new_data = empty($this->Param['place_id'])? array() : $this->Param['place_id'];
            $diff1 = empty($old_data)? array() : array_diff($old_data,$new_data);//关闭的地区
            $diff2 = empty($old_data)? $new_data : array_diff($new_data,$old_data);//新开的地区
            $result = $BranchHelper->unadmittanceEdit($diff1,$diff2,$this->Param['br_id']);
            if($result['success']){
                $MemberHelper->saveLog($this->UserConfig, '网点上牌地限制', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }else{
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'getvalue'://每月20号系统执行
            $BranchHelper->timedTask();
            break;
        default:
    }

}
